using System.Collections;

namespace MFWebSockets.Core.Utilities.Collections
{
    public class ConcurrentQueue : ICollection
    {
         Queue m_Queue;

        public ConcurrentQueue()
        {
            m_Queue = new Queue();
        }
        
        public ConcurrentQueue(IEnumerable collection)
        {
            m_Queue = new Queue();
            foreach (object o in collection) m_Queue.Enqueue(o);
        }

        public void Enqueue(object item) { m_Queue.Enqueue(item); }
        //{
            
            //lock (m_SyncRoot)
            //{
                //m_Queue.Enqueue(item);
            //}
        //}

        public bool TryDequeue(out object item)
        {
            //Locking may not be needed here
            lock (SyncRoot)
            {
                if (m_Queue.Count == 0)//Comparison with 0 is faster
                {
                    item = null;
                    return false;
                }

                item = m_Queue.Dequeue();
                return true;
            }
        }

        public void CopyTo(System.Array array, int index)
        {
            lock (SyncRoot) m_Queue.CopyTo(array, index);
        }

        public int Count
        {
            get { lock (SyncRoot) return m_Queue.Count; }
        }

        public bool IsSynchronized
        {
            get { return true; }
        }

        public object SyncRoot
        {
            get { return m_Queue.SyncRoot; }
        }

        public IEnumerator GetEnumerator()
        {
            lock (SyncRoot) return m_Queue.GetEnumerator();
        }
    }

}
